package com.lin.fengxiu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lin.fengxiu.util.GenericAndJson;
import com.lin.fengxiu.util.ListAndJson;
import com.lin.fengxiu.util.MapAndJson;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName Sku
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/13 16:03
 * @Version 1.0
 */
@Entity
@Setter
@Getter
@Where(clause = "delete_time is null and online = 1")
public class Sku extends BaseEntity{
    @Id
    private Long id;
    private BigDecimal price;
    private BigDecimal discountPrice;
    private Boolean online;
    private String img;
    private String title;
    private Long spuId;

    // 获取真实价格
    public BigDecimal getActualPrice(){
        return discountPrice == null? this.price:this.discountPrice;
    }

    // 获取当前sku的规格
    @JsonIgnore
    public List<String> getSpecValueList(){
        return this.getSpecs().stream().map(Spec::getValue).collect(Collectors.toList());
    }

//    @Convert(converter = ListAndJson.class)
//    private List<Object> specs;

    private String specs;

    public List<Spec> getSpecs() {
        if(this.specs == null){
            return Collections.emptyList();
        }
        return GenericAndJson.jsonToObject(this.specs, new TypeReference<List<Spec>>() {
        });
//        return GenericAndJson.jsonToList(this.specs);
    }

    public void setSpecs(List<Spec> specs) {
        if(specs.isEmpty()){
            return ;
        }
        this.specs = GenericAndJson.objectToJson(specs);
    }

    //    @Convert(converter = MapAndJson.class)
//    private Map<String, Object> test;
    private String code;
    private Long stock;
    private Long categoryId;
    private Long rootCategoryId;

    /**
     *
     * @return List<Spec> 返回给前端的是一个List的 Json 格式的数据，这一过程需要进行处理，将私有的 specs 字符串序列化为 json
     */
//    public List<Spec> getSpecs(){
//        String specs = this.specs;
//        return null;
//    }

    /**
     *
     * @param data： specs 数据，传入的是 List 的json 数据，需要转换为 String 字符串
     */
//    public void setSpecs(List<Spec> data){
//
//    }
}
